package com.ziyear.ssm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ziyear.ssm.bean.BaseDict;
import com.ziyear.ssm.bean.Customer;
import com.ziyear.ssm.bean.QueryVo;
import com.ziyear.ssm.service.BaseDictService;
import com.ziyear.ssm.service.CustomerService;
import com.ziyear.ssm.utils.Page;

@Controller
@RequestMapping("/customer")
public class CustomerController {

	@Autowired
	private BaseDictService baseDictService;
	@Autowired
	private CustomerService customerService;
	//加载页面初始化配置文件,下拉列表显示配置
	@Value("${customer.source.code}")
	private String custSourceCode;
	@Value("${customer.industry.code}")
	private String custIndustryCode;
	@Value("${customer.level.code}")
	private String custLevelCode;
	
	@RequestMapping("showlist.action")
	public String showList(QueryVo queryVo,Model model) {
		
		//初始化客户来源信息
		List<BaseDict> sourceList = baseDictService.getDictListByTypeCode(custSourceCode);
		//初始化客户行业信息
		List<BaseDict> industyList = baseDictService.getDictListByTypeCode(custIndustryCode);
		//初始化客户级别信息
		List<BaseDict> levelList = baseDictService.getDictListByTypeCode(custLevelCode);
		//把初始化信息传递到页面
		model.addAttribute("fromType", sourceList);
		model.addAttribute("industryType", industyList);
		model.addAttribute("levelType", levelList);
		//用户数据并分页显示,使用封装的Page类
		Page<Customer> page = customerService.getCustomerPage(queryVo);
		model.addAttribute("page", page);
		//信息回显
		model.addAttribute("custName", queryVo.getCustName());
		model.addAttribute("custSource", queryVo.getCustSource());
		model.addAttribute("custIndustry", queryVo.getCustIndustry());
		model.addAttribute("custLevel", queryVo.getCustLevel());
		
		return "customer";
	}
	
	//根据用户id查询用户信息
	@RequestMapping("edit.action")
	@ResponseBody
	public Customer getCustomerById(Long id) {
		Customer customer = customerService.getCustomerById(id);
		return customer;
	}
	
	//保存用户信息
	@RequestMapping("update.action")
	@ResponseBody
	public String updateCustomer(Customer customer) {
		customerService.updateCustomer(customer);
		return "ok";
	}
	
	@RequestMapping("delete.action")
	@ResponseBody
	public String deleteCustomerById(Long id) {
		customerService.deleteCustomerById(id);
		return "ok";
	}
	
	//批量删除
	@RequestMapping("batchDelete.action")
	@ResponseBody
	public String batchDelete(String ids) {
		customerService.batchDelete(ids);
		return "ok";
	}
	
}
